package com.yhsoft.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.bill.controller.request.SalerSkInVo;
import com.yhsoft.bill.controller.request.SalerYszkInVo;
import com.yhsoft.bill.controller.response.SalerYszkOutVo;
import com.yhsoft.bill.entity.SalerYszk;
import com.yhsoft.bill.mapper.SalerYszkMapper;
import com.yhsoft.bill.service.ISalerYszkService;
import com.yhsoft.common.service.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 * 渠道商应收账款 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
public class SalerYszkServiceImpl extends ServiceImpl<SalerYszkMapper, SalerYszk> implements ISalerYszkService {

    @Override
    public void addYszk(String customerId) {
        SalerYszk yszk = new SalerYszk();
        yszk.setCompanyId(TokenService.getUserBytoken().getCompanyId());
        yszk.setCustomerId(Integer.valueOf(customerId));
        yszk.setTotalYsAmount(BigDecimal.ZERO);
        yszk.setTotalPayAmount(BigDecimal.ZERO);
        yszk.setNeedPayAmount(BigDecimal.ZERO);
        baseMapper.insert(yszk);
    }

    @Override
    public void updateYszk(Integer customerId, BigDecimal multiply) {
        LambdaQueryWrapper<SalerYszk> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SalerYszk::getCustomerId, customerId);
        SalerYszk yszk = baseMapper.selectOne(queryWrapper);
        yszk.setTotalYsAmount(yszk.getTotalYsAmount().add(multiply));
        yszk.setNeedPayAmount(yszk.getNeedPayAmount().add(multiply));
        baseMapper.updateById(yszk);
    }

    @Override
    public IPage<SalerYszkOutVo> getYszkList(SalerYszkInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<SalerYszkOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(inVo.getCustomerId()), "a.customer_id", inVo.getCustomerId());
        wrapper.eq("a.company_id", TokenService.getUserBytoken().getCompanyId());

//    GT(">"),
//    GE(">="),
//    LT("<"),
//    LE("<=")
        // 查询条件
        wrapper.ge((inVo.getStartTime() != null), "a.updated_time", inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le( "a.updated_time", LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }
        if(StringUtils.hasLength(inVo.getIsSettled())){
            if(inVo.getIsSettled().equals("0")){
                wrapper.eq(StringUtils.hasLength(inVo.getIsSettled()), "a.need_pay_amount", 0);
            }else if(inVo.getIsSettled().equals("1")){
                wrapper.gt(StringUtils.hasLength(inVo.getIsSettled()), "a.need_pay_amount", 0);
            }
        }

        wrapper.orderByDesc("a.create_time");

        Page<SalerYszkOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SalerYszkOutVo> iPage = baseMapper.getYszkList(page, wrapper);

        return iPage;

    }

    @Override
    public void sk(SalerSkInVo inVo) {
        // 判断是否已经有记录
        LambdaQueryWrapper<SalerYszk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SalerYszk::getCompanyId, TokenService.getUserBytoken().getCompanyId());
        List<SalerYszk> list = this.baseMapper.selectList(wrapper);
        if (list.size() == 0) {
            return;
        }
        SalerYszk salerYszk = list.get(0);

        // 添加已收账款
        BigDecimal totalPayAmount = salerYszk.getTotalPayAmount();
        totalPayAmount = totalPayAmount.add(inVo.getPayAmount());

        salerYszk.setTotalPayAmount(totalPayAmount);
        salerYszk.setNeedPayAmount(salerYszk.getNeedPayAmount().subtract(inVo.getPayAmount()));

        this.baseMapper.updateById(salerYszk);

    }

}
